<?php 
class CurrencyExchangeController extends Controller{
	public function __construct(){
		$this->calledClass(get_class($this));
	}
	public function DisplayAdmin(){
		$currencyexchange = $this->loadModel("CurrencyExchange");
		$this->set(array("fromcurrency"=>$currencyexchange->FromCurrency()));
		$this->set(array("selectallcurrency"=>$currencyexchange->SelectAllCurrency()));
		$query = $currencyexchange->queryJoinTable();
		$this->set(array("list"=>$currencyexchange->DisplayAdmin($query,$this->get("page"),5,array("from_currency"=>$this->get("fromcur"),"to_currency"=>$this->get("tocurrency"),"rate"=>$this->get("rate")),array("created"),"DESC","AND")));
		$this->set(array("urls"=>array("fromcur"=>$this->get("fromcur"),"tocurrency"=>$this->get("tocurrency"),"rate"=>$this->get("rate"))));
		$this->set(array("getAllCurrencyExchange"=>$currencyexchange->GetAllCurrencyExchange()));
		$this->render("admin");
	}
	public function save(){
		$currencyexchange = $this->loadModel("CurrencyExchange");
		$this->set(array("list"=>$currencyexchange->SelectAllCurrency()));
		$querycompare = $currencyexchange->QueryOneRecord(
						array("rate ="=>$this->get("txtRate"),
							  "from_currency ="=>$this->get("txtFromCurrency"),
							  "to_currency ="=>$this->get("txtToCurrency")));
		if($querycompare == NULL){
			$currencyexchange->save(array(
								"rate"=>$this->get("txtRate"),
								"from_currency"=>$this->get("txtFromCurrency"),
								"to_currency"=>$this->get("txtToCurrency"),
								"currency_id"=>$this->get("txtCurrency"),
								"version"=>1,
								"created"=>date("Y-m-d H:i:s")));
		}else{
			if($querycompare->rate == $this->get("txtRate") || ($querycompare->from_currency == $this->get("txtFromCurrency") && $querycompare->to_currency == $this->get("txtToCurrency"))){
				?>
				<script type="text/javascript">
					alert("You input duplicate data");
				</script>
				<?php
			}else{
				$currencyexchange->save(array(
						"rate"=>$this->get("txtRate"),
						"from_currency"=>$this->get("txtFromCurrency"),
						"to_currency"=>$this->get("txtToCurrency"),
						"currency_id"=>$this->get("txtCurrency"),
						"version"=>1,
						"created"=>date("Y-m-d H:i:s")));
			}	
		
		}
		$this->AddNew();
	}
	public function AddNew(){
		$currencyexchange = $this->loadModel("CurrencyExchange");
		$this->set(array("fromcurrency"=>$currencyexchange->FromCurrency()));
		$this->set(array("selectallcurrency"=>$currencyexchange->SelectAllCurrency()));
		$this->set(array("layout"=>"addnew"));
		$this->render("admin");
	}
	public function GetField(){
		$currencyexchange = $this->loadModel("CurrencyExchange");
			$checkedbox = $this->get("pid");
			if($checkedbox){
				$query = $currencyexchange->queryJoinTable();
				$getfield= $currencyexchange->GetFieldWithMultiTable($query,array("CurrencyExchanges.id="=>$checkedbox));
				$this->set(array("getfield"=>$getfield));
				$this->set(array("fromcurrency"=>$currencyexchange->FromCurrency()));
				$this->set(array("selectallcurrency"=>$currencyexchange->SelectAllCurrency()));
				$this->set(array("layout"=>"edit"));
				$this->render("admin");
			}else {
				$this->DisplayAdmin();
		}
	}
	public function delete(){
		$currencyexchange = $this->loadModel("CurrencyExchange");
			$checkedbox = $this->get("id");
			if($checkedbox){
				$currencyexchange->delete(array($checkedbox),true);
				$this->DisplayAdmin();
			}else {
				$this->DisplayAdmin();
		}
	}
	public function SaveEdit(){
		$currencyexchange = $this->loadModel("CurrencyExchange");
		$checkedbox = $this->get("checkbox");
		$rate = $this->get("txtRate");
		$fromcurrency = $this->get("txtFromCurrency");
		$tocurrency = $this->get("txtToCurrency");
		$currency = $this->get("txtCurrency");
		if($checkedbox){
			$data = $currencyexchange->version("CurrencyExchanges","version",array("id="=>$checkedbox));
			$version = $data + 1;
			$currencyexchange->edit(array(
						"rate"=>ucwords(trim($rate)),
						"from_currency"=>ucwords(trim($fromcurrency)),
						"to_currency"=>ucwords(trim($tocurrency)),
						"currency_id"=>ucwords(trim($currency)),
						"version"=>$version,
						"modify"=>date("Y-m-d H:i:s")),array("id="=>$checkedbox));
		$this->DisplayAdmin();
		}else{
			$this->DisplayAdmin();
		}
	}
}
?>